/    Sign up×
Community /Pin to ProfileBookmark

Flash game high Scores PHP – Help?

Hi im a complete beginer to PHP & MySQL so hop someone here can help!

I am adapting a flash game of tetris i have found on the internet and everything is fine apart from the fact it wont add the high scores & show the high scores from a MySQL database (not a text file).

im not sure what the problem is and cant seam to sort it out. the unmodified version works ok when linking to the defauld scores on his server but when you try to set it up on your own server it stops working!

if some one would be so kind to have alook at it for me i would be most greatful

the original files are here… [url]http://www.neave.com/games/tetris/tetris_fla.zip[/url]

thanks in advance

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@funkdawebauthorJun 28.2005 — lobo thanks thats great but im not after how to do it im after help on how to get the one im using to work as the game was pre-made and ive just modified it to look how i want it to so all the variables etc.. are already set!
Copy linkTweet thisAlerts:
@BeachSideJun 28.2005 — Well Flash can only send the variables out a couple of ways. The way I have checked in the past is make your php page echo all the post variables and see which ones are the ones you need. This of course will only work if it is not sending them to his server

EDIT:: Whoops just realized you posted the fla I'll look into that and see what's going on
Copy linkTweet thisAlerts:
@funkdawebauthorJun 28.2005 — right folks ive managed to change it so it uses the text file rather than a MySQL database. I ca get it to pull the high scores out of the text file but cant seam to get it to write to it!

the section of PHP it gets stuck at is this one....

[COLOR=Navy] # Write new scores

if (strlen($newline) > 0) {

ftruncate($file, 0);

rewind($file);

fwrite($file, $newline) or error_msg('Could not save score.');

}



echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else error_msg('Could not save score.');

fclose($file);

}[/COLOR]


the actual line error it pulls up is this one....

[COLOR=Navy]else error_msg('Could not save score.');[/COLOR]

any ideas????
Copy linkTweet thisAlerts:
@ShmohelJun 28.2005 — 
[COLOR=Navy]else error_msg('Could not save score.');[/COLOR]
[/QUOTE]


[code=php]
else {error_msg('Could not save score.');}
[/code]


missing brackets.
Copy linkTweet thisAlerts:
@ShmohelJun 28.2005 — Now that I am looking at this, there are two [b]}[/b] in your script that seem to be out of place. Why are those there? Is there a part of the script that is missing?
Copy linkTweet thisAlerts:
@BeachSideJun 28.2005 — The brackets are only part of the problem it is the missing $ that is the major problem it needs to be $error_msg or else it will think that error_msg is a constant
Copy linkTweet thisAlerts:
@funkdawebauthorJun 29.2005 — thanks for your help folks but still not working! i havent wrote the original code so not sure why there a things missing etc....

here is the full code so you can see how it works...

[code=php]<?php

$name_max = 16; # Maximum player name length allowed
$display_max = 10; # Maximum number of scores to display (multiple of 10)


# Error handler
function error_msg($msg) {
exit("success=0&errorMsg=$msg");
}

# Store POSTed info
@$player_name = $_POST['name'];
@$player_score = $_POST['score'];
@$game_name = $_POST['game'];

# Need game name
if (!isset($game_name)) error_msg('Could not access scores.');

# Filename of text file to hold the scores given by game name - e.g. Pac-Man becomes pacman_scores.txt
$filename = $game_name . '_scores.txt';

# Saving new score?
if (isset($player_score) && is_numeric($player_score) && isset($player_name) && strlen($player_name) > 0 && strlen($player_name) <= $name_max) {

# Open the text file for writing
$file = fopen($filename, 'r+') or error_msg('Could not load scores.');

# Lock file
if (flock($file, LOCK_EX | LOCK_NB)) {
$content = fread($file, filesize($filename));

$newline = '';
$ranked = false;
$i = 1;

# Store pairs of values into pairs array
$pairs = explode('&', $content);
foreach ($pairs as $pair) {
# Store name or score pair
@list($nm, $val) = explode('=', $pair);
if ($i <= $display_max && strlen($val) > 0) {
# Insert name
if (substr($nm, 0, 4) == 'name') $this_name = $val;
else {
# Insert score or player's score
$this_score = $val;
if (!$ranked && ((int)$player_score) > ((int)$this_score)) {
$newline .= "&name$i=$player_name&score$i=$player_score";
$ranked = true;
$i++;
}

$newline .= "&name$i=$this_name&score$i=$this_score";
$i++;
}
}
}

# Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}

echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else error_msg('2 - Could not save score.');

fclose($file);
}
else {
# Return new scores
@$file = fopen($filename, 'r') or error_msg('Could not load scores.');
$content = fread($file, filesize($filename));
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $content;
fclose($file);
}

?>[/code]
Copy linkTweet thisAlerts:
@ShmohelJun 29.2005 — Many of your IF/ELSE statements do not have brackets {} around the contents.

[code=php]
if ($item='condition') { execute something ; }
else { execute something different; }
[/code]
Copy linkTweet thisAlerts:
@funkdawebauthorJun 29.2005 — is there anychange that you would be able to help me add the {}??? as like i said im new and to be honest i wouldnt know exactly where to put them!
Copy linkTweet thisAlerts:
@ShmohelJun 29.2005 — Try this:

[code=php]
<?php

$name_max = 16; # Maximum player name length allowed
$display_max = 10; # Maximum number of scores to display (multiple of 10)


# Error handler
function error_msg($msg) {
exit("success=0&errorMsg=$msg");
}

# Store POSTed info
@$player_name = $_POST['name'];
@$player_score = $_POST['score'];
@$game_name = $_POST['game'];

# Need game name
if (!isset($game_name)){ error_msg('Could not access scores.'); }

# Filename of text file to hold the scores given by game name - e.g. Pac-Man becomes pacman_scores.txt
$filename = $game_name . '_scores.txt';

# Saving new score?
if (isset($player_score) && is_numeric($player_score) && isset($player_name) && strlen($player_name) > 0 && strlen($player_name) <= $name_max) {

# Open the text file for writing
$file = fopen($filename, 'r+') or error_msg('Could not load scores.');

# Lock file
if (flock($file, LOCK_EX | LOCK_NB)) {
$content = fread($file, filesize($filename));

$newline = '';
$ranked = false;
$i = 1;

# Store pairs of values into pairs array
$pairs = explode('&', $content);
foreach ($pairs as $pair) {
# Store name or score pair
@list($nm, $val) = explode('=', $pair);
if ($i <= $display_max && strlen($val) > 0) {
# Insert name
if (substr($nm, 0, 4) == 'name'){ $this_name = $val; }
else {
# Insert score or player's score
$this_score = $val;
if (!$ranked && ((int)$player_score) > ((int)$this_score)) {
$newline .= "&name$i=$player_name&score$i=$player_score";
$ranked = true;
$i++;
}

$newline .= "&name$i=$this_name&score$i=$this_score";
$i++;
}
}
}

# Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}

echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else { error_msg('2 - Could not save score.'); }

fclose($file);
}
else {
# Return new scores
@$file = fopen($filename, 'r') or error_msg('Could not load scores.');
$content = fread($file, filesize($filename));
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $content;
fclose($file);
}

?>
[/code]


If that does not work, post the error message you receive.
Copy linkTweet thisAlerts:
@funkdawebauthorJun 29.2005 — again i get the same error message

[code=php] # Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}

echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else { error_msg('2 - Could not save score.'); }

fclose($file);
} [/code]


and shows this part of the code as the error

[code=php] else { error_msg('2 - Could not save score.'); } [/code]

?
Copy linkTweet thisAlerts:
@ShmohelJun 29.2005 — Do you have a function called [b]error_msg[/b] defined previously in your code. My guess is it is getting to this point and the error you see on screen is something like "calling an undefined function"? Am I right? I do not think that [b]error_msg[/b] itself is a php defined function (can someone else please verify? I have never seen it before and could not see a reference to it in php.net)

I would go through your code and take out all the places that you have [b]error_msg('text')[/b] and replace it with [b]print('text')[/b]
Copy linkTweet thisAlerts:
@funkdawebauthorJun 29.2005 — would that then pull the 'text' bit into flash like it does now? check my version out.... http://www.ilovevc.com/game/vicioustetris.swf
Copy linkTweet thisAlerts:
@funkdawebauthorJul 13.2005 — anyone?
Copy linkTweet thisAlerts:
@SpectreReturnsJul 14.2005 — Just a note to Shmohel, if you're only doing one action in a control structure, you don't need the braces.

if (cond) action;

is the same as:

if (cond) { action; }

AND

if (cond) action;

else action;

is the same as

(cond) ? action : action;


[EDIT] And while I'm here, what error are you actually getting?
Copy linkTweet thisAlerts:
@mishopMar 27.2015 — And I need help with score.

The game http://www.igrice-tigrice.com/wp-content/games/YambWeb.swf made POST to /index.php?page=save_score but I can't capture anything.
Copy linkTweet thisAlerts:
@Strider64Mar 27.2015 — You be better posting your own thread than on a thread that is 10 years old.
Copy linkTweet thisAlerts:
@sidorejoMar 29.2015 — hello, I want to ask me, beg to be answered ...

I want to add a simple flash animation on my blog to make it look more beautiful, what is there that can help ...?

This link my blog http://obattradisionalmanjur.blogspot.com/2014/10/7-obat-tradisional-kolesterol.html
Copy linkTweet thisAlerts:
@Mehak50Aug 16.2015 — I had a similar problem with my flash games online site. I got it solved after some research online. Here I got very good help from this question and the answers are outstanding.

It is really good for those who have similar kind of problem in their flash games sites. Thanks buddies.
Copy linkTweet thisAlerts:
@TerryMulhernOct 04.2019 — Hello,

I had this error message with the snake highscore script and later I found a Neave scoreboard script ranking issue with the same error on the Flashkit forum. The solution is simple:

> you are sending "name" but trying to get it by "username" ($_POST['username'])

> also do mysql_close(); before exit(); (in generally, you don't need exit() by the way).


[https://stackoverflow.com/questions/27335263/save-flash-game-scores-php-mysql-](https://stackoverflow.com/questions/27335263/save-flash-game-scores-php-mysql)[icon](https://hireessaywriter.org/)

Hope it helps, in case someone else is still looking for the fix.
Copy linkTweet thisAlerts:
@ginerjmOct 04.2019 — 14 years later?
×

Success!

Help @funkdaweb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...